package cn.com.open.questiontransfer;

import cn.com.open.itembank.common.enums.YesOrNo;
import cn.com.open.openqti.Exception.ClozeGapRequireResponseIdentifierException;
import cn.com.open.openqti.Exception.OpenPromptRequireMimeTypeException;
import cn.com.open.openqti.Exception.OpenPromptRequireSrcException;
import cn.com.open.qti.ClozeGap;
import cn.com.open.qti.Media;
import cn.com.open.qti.MediaTest;
import cn.com.open.utils.JaxbUtil;
import cn.com.open.utils.PatternUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lucky on 2017/7/6.
 */
public class MediaMaker {
    /**
     * 原始的文本信息
     */
    private String orginalString;
    /**
     * 不包含media的文本信息
     */
    private String withoutMediaString;
    /**
     * media标签
     */
    private Media media;
    /**
     * 题干中的空(用于完型填空题)
     */
    private List<ClozeGap> clozeGapCollection;

    /**
     * Gets 不包含media的文本信息.
     *
     * @return the without media string
     */
    public String getWithoutMediaString() {
        return withoutMediaString;
    }

    /**
     * Gets media标签.
     *
     * @return the media
     */
    public Media getMedia() {
        return media;
    }

    public MediaMaker(String sourse) throws ClozeGapRequireResponseIdentifierException, OpenPromptRequireSrcException, OpenPromptRequireMimeTypeException {
        orginalString = sourse;
        replaceAudioSign();
    }

    /**
     * Replace audio sign map.
     *
     * @return the map
     */
    public void replaceAudioSign() throws ClozeGapRequireResponseIdentifierException, OpenPromptRequireSrcException, OpenPromptRequireMimeTypeException {

        String resultStr = orginalString;
        //得到<img ...>的结束位置
        int a = orginalString.indexOf("media");
        int imgEndIndex = orginalString.indexOf(">", a);
        //从orginalString中得到endsign之前的所有字符
        String includeVideoStringSub = orginalString.substring(0, imgEndIndex);
        //将videoString以<media分隔成为三段
        List<String> videoStringList = PatternUtil.getPatternSplitList(includeVideoStringSub, "<media");
        //得到最后一段的内容，也就是<img..>的内容
        String mediaString = videoStringList.get(videoStringList.size() - 1);
        StringBuffer mediaStringbuff = new StringBuffer(mediaString);
        mediaString = String.format("<media%s>", mediaString);

        withoutMediaString = resultStr.replace(mediaString, "<media/>");
        /*填充media
        media = new Media();
        media.setContent(hearingcontent);
        media.setDuration(Double.valueOf(workvideotimes));
        media.setMimetype(videotype);
        media.setSrc(videourl);
        media.setValue(videocontent);
        media.setRepeatTimes(Integer.valueOf(repeatinterval));
        media.setRepeatInterval(Double.valueOf(repeatcount));*/

        List<String> mediaList = PatternUtil.splitByPattern(resultStr, PatternUtil.mediaReg);
        for (String mediaItem : mediaList){
            if(mediaItem.contains("<media")){
                if (mediaItem.indexOf("/") <= YesOrNo.NO.getIndex()) {
                    mediaItem = mediaItem.replace(mediaItem.charAt(mediaItem.length()-1) + "","/>");
                }

                MediaTest mediaTest = JaxbUtil.converyToJavaBean(mediaItem, MediaTest.class);
                Media docMedia = new Media();
                docMedia.setMediamode(mediaTest.getMediamode());
                docMedia.setSrc(mediaTest.getSrc());
                docMedia.setMimetype(mediaTest.getMediamode());
                docMedia.setDuration(mediaTest.getDuration());
                docMedia.setRepeatInterval(mediaTest.getRepeatInterval());
                docMedia.setRepeatTimes(mediaTest.getRepeatTimes());
                docMedia.setContent(mediaTest.getContent());
                docMedia.setValue(mediaTest.getValue());
                docMedia.setContentreftype(mediaTest.getContentreftype());
                if (docMedia != null) {
                    /*
                    this.media = new Media();

                    if (docMedia.getSrc() != null) {
                        this.media.setSrc(docMedia.getSrc());
                    } else {
                        throw new OpenPromptRequireSrcException();
                    }

                    if (docMedia.getMimetype() != null) {
                        this.media.setMimetype(docMedia.getMimetype());
                    } else {
                        throw new OpenPromptRequireMimeTypeException();
                    }

                    if (docMedia.getContent() != null) {
                        this.media.setContent(docMedia.getContent());
                    } else {
                        this.media.setContent("");
                    }

                    if (docMedia.getDuration() != null) {
                        this.media.setDuration(docMedia.getDuration());
                    } else {
                        this.media.setDuration(0.0);
                    }

                    if (docMedia.getRepeatInterval() != null) {
                        this.media.setRepeatInterval(docMedia.getRepeatInterval());
                    } else {
                        this.media.setRepeatInterval(0.0);
                    }

                    if (docMedia.getRepeatTimes() != null) {
                        this.media.setRepeatTimes(docMedia.getRepeatTimes());
                    } else {
                        this.media.setRepeatTimes(0);
                    }
                    */
                }
            }
        }

        this.clozeGapCollection = new ArrayList<>();
        List<String> clozeList = PatternUtil.splitByPattern(resultStr, PatternUtil.clozeReg);
        for (String clozeItem : clozeList){
            if(clozeItem.contains("<clozeGap")){
                // TODO：没想通为什么要替换掉
                //targetConvert = targetConvert.replace(clozeItem, "<clozeGap/>");

                ClozeGap docClozeGap = JaxbUtil.converyToJavaBean(clozeItem, ClozeGap.class);
                if (docClozeGap != null) {
                    ClozeGap tmpGap = new ClozeGap();
                    if (docClozeGap.getResponseIdentifier() != null) {
                        tmpGap.setResponseIdentifier(docClozeGap.getResponseIdentifier());
                        this.clozeGapCollection.add(tmpGap);
                    } else {
                        throw new ClozeGapRequireResponseIdentifierException();
                    }
                }
            }
        }
    }
}
